package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.exam.vo.ExamErrorQuestionVo;
import com.ruoyi.exam.vo.SubmitAnswerVo;
import com.ruoyi.exam.vo.SubmitAnswerResultVo;
import com.ruoyi.exam.service.IExamErrorQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户错题记录Controller
 *
 * @author Tellsea
 * @date 2025-10-27
 */
@Api(tags = "用户错题记录Controller")
@RestController
@RequestMapping("/exam/errorQuestion")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamErrorQuestionController extends BaseController {

    private final IExamErrorQuestionService examErrorQuestionService;

    @ApiOperation("查询用户错题记录列表")
    @GetMapping("/list")
    public TableDataInfo<ExamErrorQuestionVo> list(ExamErrorQuestionVo entity) {
        return examErrorQuestionService.queryList(entity);
    }

    @ApiOperation("查询用户错题记录所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamErrorQuestionVo entity) {
        return AjaxResult.success("查询成功", examErrorQuestionService.queryAll(entity));
    }

    @ApiOperation("导出用户错题记录列表")
    @Log(title = "用户错题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamErrorQuestionVo entity) {
        List<ExamErrorQuestionVo> list = examErrorQuestionService.queryAll(entity);
        ExcelUtil<ExamErrorQuestionVo> util = new ExcelUtil<>(ExamErrorQuestionVo.class);
        util.exportExcel(response, list, "用户错题记录数据");
    }

    @ApiOperation("获取用户错题记录详细信息")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examErrorQuestionService.queryById(id));
    }

    @ApiOperation("新增用户错题记录")
    @Log(title = "用户错题记录", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamErrorQuestionVo entity) {
        return toAjax(examErrorQuestionService.save(entity));
    }

    @ApiOperation("修改用户错题记录")
    @Log(title = "用户错题记录", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamErrorQuestionVo entity) {
        return toAjax(examErrorQuestionService.updateById(entity));
    }

    @ApiOperation("删除用户错题记录")
    @Log(title = "用户错题记录", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examErrorQuestionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("查询当前用户在指定题库的错题ID列表（按题库排序）")
    @GetMapping("/errorQuestionIds/{bankId}")
    public AjaxResult queryErrorQuestionIdsByBank(@PathVariable Long bankId) {
        Long userId = getUserId();
        List<Long> questionIds = examErrorQuestionService.queryErrorQuestionIdsByBank(userId, bankId);
        return AjaxResult.success("查询成功", questionIds);
    }

    @ApiOperation("错题答题提交答案（答对则删除错题记录，答错则保留）")
    @Log(title = "错题答题", businessType = BusinessType.UPDATE)
    @PostMapping("/submitAnswer")
    public AjaxResult submitErrorAnswer(@Validated @RequestBody SubmitAnswerVo submitAnswerVo) {
        try {
            Long userId = SecurityUtils.getUserId();
            SubmitAnswerResultVo result = examErrorQuestionService.submitErrorAnswer(submitAnswerVo, userId);
            return AjaxResult.success("提交成功", result);
        } catch (Exception e) {
            logger.error("提交错题答案失败", e);
            return AjaxResult.error("提交答案失败：" + e.getMessage());
        }
    }

    
}
